var collectedImages = [];
chrome.browserAction.setBadgeBackgroundColor({ color: [255, 0, 0, 255] });

chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
    if (request.imgCount && request.imgCount > 0) {
      console.log(sender.tab.url);
      console.log(request.imgCount, request.imgData);
      collectedImages = request.imgData;
      chrome.browserAction.setBadgeText({ text: request.imgCount.toString() });
      sendResponse({ result: 'ok' });
    } else {
      chrome.browserAction.setBadgeText({ text: "" });
      sendResponse({ result: 'ok' });
    }
  });

chrome.contextMenus.create({
  title: '采集页面上的图片', // %s表示选中的文字
  contexts: ['image'], // 只有当选中文字时才会出现此右键菜单
  onclick: function (params) {
    // 注意不能使用location.href，因为location是属于background的window对象
    sendMessageToContentScript({ type: 'reload', value: 'Reload request from background.' }, function (response) {
      console.log('来自content的回复：' + response);
    });
  }
});